﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//Thêm thư viện
using KaraokeManagementDTO;
using System.Data.OleDb;
using System.Data;

namespace KaraokeManagementDAO
{
    public class PhieuTinhTienDAO:DataProvider
    {
        public Boolean themPhieu(PhieuTinhTienDTO dto)
        {
            List<OleDbParameter> para = new List<OleDbParameter>();

            //Câu truy vấn thêm dữ liệu
            String query = "insert into KhachHang(NgayThanhToan,PhongHat_ID,NhanVien_ID,TongGiaTriHangHoa,GiamGia,ThueVAT,KhachHang_ID) values(@NgayThanhToan,@PhongHat_ID,@NhanVien_ID,@TongGiaTriHangHoa,@GiamGia,@ThueVAT,@KhachHang_ID)";

            para.Add(new OleDbParameter("@NgayThanhToan", dto.NgayThanhToan));
            para.Add(new OleDbParameter("@PhongHat_ID", dto.PhongHat_ID));
            para.Add(new OleDbParameter("@NhanVien_ID", dto.NhanVien_ID));
            para.Add(new OleDbParameter("@TongGiaTriHangHoa", dto.TongGiaTriHoaDon));
            para.Add(new OleDbParameter("@GiamGia", dto.GiamGia));
            para.Add(new OleDbParameter("@ThueVAT", dto.ThueVAT));
            para.Add(new OleDbParameter("@KhachHang_ID", dto.KhachHang_ID));

            if (ExecuteNoneQuery(query, para) < 0)
            {
                return false;
            }

            return true;
        }

        public Boolean xoaPhieu(int MaPhieu)
        {
            List<OleDbParameter> para = new List<OleDbParameter>();

            //Cau truy van xoa phieu
            String query = "delete from PhieuTinhTien where ID=@MaPhieu";

            para.Add(new OleDbParameter("@ID", MaPhieu));

            if (ExecuteNoneQuery(query, para) < 0)
            {
                return false;
            }

            return true;
        }

        public Boolean capNhatPhieu(PhieuTinhTienDTO dto)
        {
            List<OleDbParameter> para = new List<OleDbParameter>();

            //Cau truy van cap nhat phieu
            String query = "update PhieuTinhTien set NgayThanhToan=@NgayThanhToan,PhongHat_ID=@PhongHat_ID,NhanVien_ID=@NhanVien_ID,TongGiaTriHangHoa=@TongGiaTriHangHoa,GiamGia=@GiamGia,ThueVAT=@ThueVAT,KhachHang_ID=@KhachHang_ID where ID=@ID";

            para.Add(new OleDbParameter("@NgayThanhToan", dto.NgayThanhToan));
            para.Add(new OleDbParameter("@PhongHat_ID", dto.PhongHat_ID));
            para.Add(new OleDbParameter("@NhanVien_ID", dto.NhanVien_ID));
            para.Add(new OleDbParameter("@TongGiaTriHangHoa", dto.TongGiaTriHoaDon));
            para.Add(new OleDbParameter("@GiamGia", dto.GiamGia));
            para.Add(new OleDbParameter("@ThueVAT", dto.ThueVAT));
            para.Add(new OleDbParameter("@KhachHang_ID", dto.KhachHang_ID));
            para.Add(new OleDbParameter("@ID", dto.ID));

            if (ExecuteNoneQuery(query, para) < 0)
            {
                return false;
            }

            return true;
        }

        public PhieuTinhTienDTO layThongTinPhieu(int MaPhieu)
        {
            PhieuTinhTienDTO phieu = new PhieuTinhTienDTO();
            List<OleDbParameter> para = new List<OleDbParameter>();

            //Cau truy van lay thong tin chi tiet
            String query = "select * from PhieuTinhTien where ID=@MaPhieu";

            para.Add(new OleDbParameter("@ID", MaPhieu));

            //DataTable
            DataTable dt = new DataTable();
            dt = ExecuteQuery(query, para);

            if (dt.Rows.Count == 0)
            {
                return null;
            }

            DataRow row = dt.Rows[0];

            phieu.ID = int.Parse(row["ID"].ToString());
            phieu.NgayThanhToan = DateTime.Parse(row["NgayThanhToan"].ToString());
            phieu.PhongHat_ID = int.Parse(row["PhongHat_ID"].ToString());
            phieu.NhanVien_ID = int.Parse(row["NhanVien_ID"].ToString());
            phieu.TongGiaTriHoaDon = Double.Parse(row["TongGiaTriHangHoa"].ToString());
            phieu.GiamGia = Double.Parse(row["TongGiaTriHangHoa"].ToString());
            phieu.ThueVAT = Double.Parse(row["ThueVAT"].ToString());
            phieu.KhachHang_ID = int.Parse(row["KhachHang_ID"].ToString());

            return phieu;
        }

        public List<PhieuTinhTienDTO> layDanhSachPhieu()
        {
            List<PhieuTinhTienDTO> listPhieu = new List<PhieuTinhTienDTO>();

            List<OleDbParameter> para = new List<OleDbParameter>();

            //Câu truy vấn lấy dữ liệu
            String query = "select * from PhieuTinhTien";


            //DataTable
            DataTable dt = new DataTable();
            dt = ExecuteQuery(query, para);

            if (dt.Rows.Count == 0)
            {
                return null;
            }

            foreach (DataRow row in dt.Rows)
            {
                PhieuTinhTienDTO dto = new PhieuTinhTienDTO();

                dto.ID = int.Parse(row["ID"].ToString());
                dto.NgayThanhToan = DateTime.Parse(row["NgayThanhToan"].ToString());
                dto.PhongHat_ID = int.Parse(row["PhongHat_ID"].ToString());
                dto.NhanVien_ID = int.Parse(row["NhanVien_ID"].ToString());
                dto.TongGiaTriHoaDon = Double.Parse(row["TongGiaTriHangHoa"].ToString());
                dto.GiamGia = Double.Parse(row["TongGiaTriHangHoa"].ToString());
                dto.ThueVAT = Double.Parse(row["ThueVAT"].ToString());
                dto.KhachHang_ID = int.Parse(row["KhachHang_ID"].ToString());
                listPhieu.Add(dto);
            }

            return listPhieu;
        }
    }
}
